//获取页面跳转传过来的参数值
function request(params) {

    var url = location.href;

    var paraString = url.substring(url.indexOf("?") + 1, url.length).split("&");

    var paraObj = {};

    for (i = 0; j = paraString[i]; i++) {

        paraObj[j.substring(0, j.indexOf("=")).toLowerCase()] = j.substring(j.indexOf("=") + 1, j.length);

    }

    var returnValue = paraObj[params.toLowerCase()];

    if (typeof (returnValue) == "undefined") {

        return "";

    } else {

        return returnValue;

    }

}
//userId为登录成功后返回的userId,表示当前访问这个页面的登录用户id，
var userId;
$(document).ready(function(){
    userId=window.sessionStorage.getItem(loginUserId);
    $("#user_detail").attr("readonly","readonly");
});

var url = 'http://www.xiaowenxiong.cn/test';
var sex;
function conwrite() {
    $("#user_detail").removeAttr("readonly");
    $(".con-write").css("display", "block");
    $(".con").hide();
    if ($("#user_detail").val() == "这个家伙很懒，什么都没留下！！！！") {
        $("#user_detail").val("");
    }
    //0男 1女
    if (sex == 1) {
        $("#nan").removeAttr("checked");
        $("#nv").attr("checked", "checked");
    } else if (sex == 0) {
        $("#nv").removeAttr("checked");
        $("#nan").attr("checked", "checked");
    }
}
function con() {
    $("#user_detail").attr("readonly","readonly");
    $(".con").css("display", "block");
    $(".con-write").css("display", "none");
    //封装对象
    var user = {};
    user.userId = userId;
    user.userPhone = $("#re_user_phone").val();
    user.userBirthday = $("#re_user_birthday").val();
    user.userDetail = $("#user_detail").val();
    user.userSex = $('input:radio[name="sex"]:checked').val();
    if (imagename != 0) {
        user.userAvatarAddress = imagename;
    }
    $.ajax({
        async: "true",
        type: 'POST',
        url: url + '/users/details/revise',
        contentType: 'application/json;charset=utf-8',
        dataType: 'json',
        traditional: true,
        data: JSON.stringify(user),
        success: function (result) {
            if (result.error == 0) {
                showUserInfo();
            } else {
                alert('修改个人资料失败');
            }
        },
        error: function (xhr, type) {
            alert('请求出错，再刷新试试');
        }
    });

}

//用户详情信息展示
function showUserInfo() {
    //用户id
    $.ajax({
        type: 'GET',
        url: url + '/users/details/show',
        data: {
            userId: userId
        },
        dataType: 'json',
        timeout: 50000,
        success: function (result) {
            //alert(result.data);
            console.log(result);
            if (result.error == 0) {

                var data = result.data;
                $("#report_travels").attr('href', '../travel-note/note-write.html');
                $("#user_email").text(data.userEmail);
                $("#user_phone").text(data.userPhone);
                $("#re_user_phone").val(data.userPhone);

                if (data.hasOwnProperty("userBirthday")) {
                    $("#user_birthday").text(dateFormat_1(data.userBirthday));
                    $("#re_user_birthday").val(dateFormat_1(data.userBirthday));
                }
                //判断json是否有某个key
                //result.data.hasOwnProperty("key")
                if (data.hasOwnProperty("userSex") && data.userSex == 1) {
                    sex = 1;
                    $("#user_sex").text("女");

                } else if (data.hasOwnProperty("userSex") && data.userSex == 0) {
                    sex = 0;
                    $("#user_sex").text("男");

                }
                if (!data.hasOwnProperty("userDetail") || data.userDetail == '') {
                    $("#user_detail").text("这个家伙很懒，什么都没留下！！！！");
                } else {
                    $("#user_detail").val(data.userDetail);
                }
                if (data.hasOwnProperty("userAvatarAddress") && data.userAvatarAddress != '') {

                    $("#user_avt").attr('src', "http://www.xiaowenxiong.cn/test/resources/img/" + data.userAvatarAddress);
                }
                //返回yyyy-MM-dd格式日期（一）：
                function dateFormat_1(longTypeDate) {
                    var dateType = "";
                    var date = new Date();
                    date.setTime(longTypeDate);
                    dateType = date.getFullYear() + "-" + getMonth(date) + "-" + getDay(date);//yyyy-MM-dd格式日期
                    return dateType;
                }

                //返回 01-12 的月份值
                function getMonth(date) {
                    var month = "";
                    month = date.getMonth() + 1; //getMonth()得到的月份是0-11
                    if (month < 10) {
                        month = "0" + month;
                    }
                    return month;
                }

                //返回01-30的日期
                function getDay(date) {
                    var day = "";
                    day = date.getDate();
                    if (day < 10) {
                        day = "0" + day;
                    }
                    return day;
                }
            } else {
                alert("数据获取出错了")
            }
        },
        error: function (xhr, type) {

            alert('请求出错，再刷新试试');
        }
    });
}
//删除游记
function deleteTravels(travelId) {
    if (confirm("确定删除数据")) {
        $.ajax({
            type: 'GET',
            url: url + '/travels/delete',
            data: {
                travelId: travelId
            },
            dataType: 'json',
            timeout: 50000,
            success: function (result) {
                if (result.error == 0) {
                    $('#li_' + travelId + '').remove();
                } else {
                    alert('游记删除失败，请重试');
                }
            },
            error: function (xhr, type) {
                alert('请求出错，再刷新试试');
            }
        });
    }
}
//个人游记展示
function showTravels() {
    $.ajax({
        type: 'GET',
        url: url + '/travels/listTravels',
        data: {
            userId: userId
        },
        dataType: 'json',
        timeout: 50000,
        success: function (result) {

            if (result.error == 0) {
                var data = result.data;
                console.log(data);
                for (var i = 0; i < data.length; i++) {
                    $("#travel_ul").append(" <li id='li_" + data[i].travleId + "' class='col-md-12 list-group-item'  travleId=" + data[i].travleId + ">" +
                        "<a class='col-md-4' href='../travel-note/note-content.html?travleId=" + data[i].travleId + "&userId="+userId+"'>" + data[i].travelTitle + "</a>" +
                        "<div class='col-md-7'>" +
                        "<span class='col-md-3'  style='color: rgb(73, 75, 73); font-size: 15px;'>" + data[i].travelType.travelTypeContent + "</span>" +
                        "<span class='col-md-1 glyphicon glyphicon-eye-open' style='color: rgb(73, 75, 73); font-size: 15px;'>" + data[i].travelPageView + "</span>" +
                        "<span class='col-md-1 glyphicon glyphicon-thumbs-up' style='color: rgb(73, 75, 73);font-size: 15px;'>" + data[i].travelLikeNum + "</span>" +
                        "<span class=' col-md-7 glyphicon glyphicon-time' style='color: rgb(73, 75, 73); font-size: 12px;'>" + datetimeFormat_2(data[i].travelIssueTime) + "</span>" +

                        "</div>"+
                        "<button type='button' class='col-md-1 btn btn-default' onclick=deleteTravels(" + data[i].travleId + ")>删除</button>" +
                        "</li>");
                }
                //去点默认的li节点
                $("#travel_default").remove();
            } else if (result.error == 112) {
                $("#travel_default").text("啥都没写，来写点");
            } else {
                $("#travel_default").text("数据获取出错了");
            }

        },
        error: function (xhr, type) {
            alert('请求出错，再刷新试试');
        }
    });
}

//返回yyyy-MM-dd 00:00:00格式日期
function datetimeFormat_2(longTypeDate) {
    var datetimeType = "";
    var date = new Date();
    date.setTime(longTypeDate);
    datetimeType = date.getFullYear() + "-" + getMonth(date) + "-" + getDay(date) + " " + getHours(date) + ":" + getMinutes(date) + ":" + getSeconds(date);//yyyy-MM-dd 00:00:00格式日期
    return datetimeType;
}
//返回 01-12 的月份值
function getMonth(date) {
    var month = "";
    month = date.getMonth() + 1; //getMonth()得到的月份是0-11
    if (month < 10) {
        month = "0" + month;
    }
    return month;
}
//返回01-30的日期
function getDay(date) {
    var day = "";
    day = date.getDate();
    if (day < 10) {
        day = "0" + day;
    }
    return day;
}
//返回小时
function getHours(date) {
    var hours = "";
    hours = date.getHours();
    if (hours < 10) {
        hours = "0" + hours;
    }
    return hours;
}
//返回分
function getMinutes(date) {
    var minute = "";
    minute = date.getMinutes();
    if (minute < 10) {
        minute = "0" + minute;
    }
    return minute;
}
//返回秒
function getSeconds(date) {
    var second = "";
    second = date.getSeconds();
    if (second < 10) {
        second = "0" + second;
    }
    return second;
}

$(document).ready(function () {


    showTravels();


    showUserInfo();


});